package com.lill.product.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.util.IdUtil;
import com.lill.product.dao.IBaseDiscountDao;
import com.lill.product.dao.IProductCarDao;
import com.lill.product.dao.IProductDiscountDao;
import com.lill.product.model.BaseDiscountModel;
import com.lill.product.model.ProductCarModel;
import com.lill.product.service.IProductDiscountService;

/**
 * 车辆返利值Service实现类
 *
 * @author lill
 * @date 2021-09-06 20:23:17
 */
@Service("com.lill.product.service.impl.ProductDiscountServiceImpl")
public class ProductDiscountServiceImpl implements IProductDiscountService {
	
    @Autowired
    private IProductDiscountDao productDiscountDao;
    @Autowired
    private IBaseDiscountDao baseDiscountDao;
    @Autowired
    private IProductCarDao productCarDao;

    public Map<String, Object> queryObject(String productId) {
    	Map<String, Object> discountMap=productDiscountDao.queryObject(productId);
    	if(null!=discountMap){
    		discountMap.put("productId", discountMap.get("product_id"));
    	}
        return discountMap;
    }
    
    public List<Map<String, Object>> queryList(Map<String, Object> map) {
    	Map<String, Object> mapCol=new HashMap<String, Object>();
    	mapCol.put("companyId", map.get("companyId"));
    	if(map.containsKey("branchId")){
    		mapCol.put("branchId", map.get("branchId"));
    	}else{
    		mapCol.put("branchIdIsNull", map.get("branchIdIsNull"));
    	}
    	List<BaseDiscountModel> baseDiscountList=baseDiscountDao.queryList(mapCol);
    	if(CollectionUtils.isNotEmpty(baseDiscountList)){
    		List<String> colNameList=new ArrayList<String>();
    		for (BaseDiscountModel baseDiscount : baseDiscountList) {
    			colNameList.add(baseDiscount.getMapName());
			}
    		map.put("colNameList", colNameList);
    	}
    	List<Map<String, Object>> mapList=productDiscountDao.queryList(map);
    	if(CollectionUtils.isNotEmpty(mapList)){
    		Map<Object, ProductCarModel> carModelMap=new HashMap<Object, ProductCarModel>();
    		for (Map<String, Object> mapDiscount : mapList) {
    			if(carModelMap.containsKey(mapDiscount.get("product_id"))){
    				mapDiscount.put("carModel", carModelMap.get(mapDiscount.get("product_id")));
    			}else{
    				ProductCarModel carModel=productCarDao.queryObject(mapDiscount.get("product_id"));
    				mapDiscount.put("carModel", carModel);
    				carModelMap.put(mapDiscount.get("product_id"), carModel);
    			}
			}
    	}
        return mapList;
    }

    
    public int queryTotal(Map<String, Object> map) {
        return productDiscountDao.queryTotal(map);
    }

    public int save(Map<String, Object> discountMap) {
    	int num=productDiscountDao.queryTotal(discountMap);
    	if(discountMap.containsKey("productId")){
    		discountMap.put("product_id", discountMap.get("productId"));
    		discountMap.remove("productId");
    	}
    	if(num>0){
    		return productDiscountDao.update(discountMap);
    	}else{
    		discountMap.put("id", IdUtil.createIdbyUUID());
    		return productDiscountDao.save(discountMap);
    	}
    }

}
